package com.whiteape.book.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.whiteape.book.R;
import com.whiteape.book.ui.base.BaseActivity;
import com.whiteape.book.ui.fragment.MediaCollectionFragment;
import com.whiteape.book.utils.LogUtils;
import com.whiteape.book.widget.itemdecoration.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ChannelManageActivity extends BaseActivity {
    @BindView(R.id.rv_content)
    RecyclerView mRecyclerView;

    ItemDragAdapter itemDragAdapter;

    MyAdapter myAdapter;
    @Override
    protected int getContentId() {
        return R.layout.activity_channel_manage;
    }

    @Override
    protected void setUpToolbar(Toolbar toolbar) {
        super.setUpToolbar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("书架顺序调整");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
        toolbar.setNavigationOnClickListener(v -> {
            exit();
        });
    }

    @Override
    protected void initView() {
        super.initView();
//        mRecyclerView.setAdapter(myAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this));

        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(itemDragAdapter);
        itemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);

        itemDragAdapter.enableDragItem(itemTouchHelper, R.id.iv_manage, true);
        itemDragAdapter.setOnItemDragListener(new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder viewHolder, int from, RecyclerView.ViewHolder viewHolder1, int to) {
                LogUtils.e("onItemDragMoving, from " + from + " to " + to);

            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {

            }
        });
        mRecyclerView.setAdapter(itemDragAdapter);

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        List<String> channels = getIntent().getStringArrayListExtra("channelList");
//        channels.add("小说");
//        channels.add("漫画");
        itemDragAdapter = new ItemDragAdapter(R.layout.item_channel_manage, channels);
        myAdapter = new MyAdapter(R.layout.item_channel_manage, channels);
//        mRecyclerView.setAdapter(itemDragAdapter);

    }

    class ItemDragAdapter extends BaseItemDraggableAdapter<String, BaseViewHolder> {
        public ItemDragAdapter(int layoutResId, List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.tv_channel_name, item);

        }
    }

    class MyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        public MyAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {

            helper.setText(R.id.tv_channel_name, item);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void exit() {
        List<String> data = itemDragAdapter.getData();
        Intent intent = new Intent();
        intent.putStringArrayListExtra("channelList", new ArrayList<>(data));
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
    @Override
    public void onBackPressed() {
        exit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            exit();
        }
        return super.onOptionsItemSelected(item);
    }
}
